package LinkedList

import (
	"fmt"
)

type List struct {
	head  *Node
	tail  *Node
	count uint
}

type Node struct {
	data int
	pre  *Node
	next *Node
}

func NewList() *List {
	l := List{}
	l.head = nil
	l.tail = nil
	l.count = 0
	return &l
}

func (l *List) removeOnlyOne() {
	if l.onlyOne() {
		l.head = nil
		l.tail = nil
		l.count = 0
	}
}
func (l *List) onlyOne() bool {
	return l.count == 1
}
func (l *List) addCount() {
	l.count++
}
func (l *List) subCount() {
	l.count--
}

//IsEmpty 空
func (l *List) IsEmpty() bool {
	return l.count == 0
}

//Size 节点数量
func (l *List) Size() uint {
	return l.count
}

//InsertFront 在头部增加
func (l *List) InsertFront(newdata int) {
	if l.head == nil {
		l.head = &Node{data: newdata, pre: nil, next: nil}
		l.tail = l.head
	} else {
		node := &Node{data: newdata, next: l.head}
		l.head.pre = node
		l.head = node
	}
	l.addCount()
}

//InsertEnd 在尾部增加
func (l *List) InsertEnd(newdata int) {
	if l.tail == nil {
		l.tail = &Node{data: newdata, pre: nil, next: nil}
		l.head = l.tail
	} else {
		node := &Node{data: newdata, pre: l.tail}
		l.tail.next = node
		l.tail = node
	}
	l.addCount()
}

//Insert 在指定位置插入
func (l *List) Insert(newdata int, index uint) {
	if index > l.count {
		return
	}
	var insertNode *Node
	insertNode = l.head
	var i uint
	for ; i < index; i++ {
		insertNode = insertNode.next
	}
	newNode := &Node{data: newdata, pre: insertNode, next: insertNode.next}
	insertNode.next, insertNode.next.pre = newNode, newNode
	l.addCount()
}

//Remove 删除指定位置元素
func (l *List) Remove(index uint) {
	if index >= l.count {
		return
	}
	if l.onlyOne() {
		l.removeOnlyOne()
		return
	}

	delNode := l.head
	var i uint
	for ; i < index; i++ {
		delNode = delNode.next
	}
	preNode := delNode.pre
	nextNode := delNode.next

	if preNode == nil {
		l.RemoveHead()
		return
	}
	if nextNode == nil {
		l.RemoveTail()
		return
	}

	preNode.next = nextNode
	nextNode.pre = preNode
	l.subCount()
}

//RemoveHead 删除头部
func (l *List) RemoveHead() {
	if l.head == nil {
		return
	}
	if l.onlyOne() {
		l.removeOnlyOne()
		return
	}
	newHead := l.head.next
	newHead.pre = nil
	l.head = newHead

	l.subCount()
}

//RemoveTail 删除尾部元素
func (l *List) RemoveTail() {
	if l.tail == nil {
		return
	}
	if l.onlyOne() {
		l.removeOnlyOne()
		return
	}
	newTail := l.tail.pre
	newTail.next = nil
	l.tail = newTail
	l.subCount()
}

//Update 修改旧数据改
func (l *List) Update(oldIndex uint, newData int) bool {
	if oldIndex >= l.count {
		return false
	}

	var i uint
	findNode := l.head
	for ; i < oldIndex; i++ {
		findNode = findNode.next
	}
	findNode.data = newData

	return true
}

//Find 查看数据
func (l *List) Find(data int) (node *Node, exist bool) {
	node = nil
	exist = false
	visitNode := l.head
	for visitNode != nil {
		if visitNode.data == data {
			exist = true
			node = visitNode
			break
		}
		visitNode = visitNode.next
	}
	return
}

//Visit 遍历
func (l *List) Visit() {
	fmt.Printf("list %d\n", l.count)
	node := l.head
	for {
		if node == nil {
			break
		}

		fmt.Printf("%d \n", node.data)

		node = node.next
	}
}
